<?php
class Model_Admin_Post extends Model{

    public function getInfo($email,$album,$title,$offset,$pageSize){

        $sql="select count(1)total
             from post t
             left join site_user s on s.site_userid=t.userid
             left join album a on a.albumid=t.album
             where t.isdeleted<>'1'
             and t.title like '%$title%'
             and s.user_email like '%$email%'
             and a.caption like '%$album%'";

        $total=DB::query(Database::SELECT,$sql)->execute()->get("total");

        $sql="select t.postid id,t.userid,s.user_email,t.album albumid,a.caption album,t.title,t.description,t.tags,t.images,t.url,t.price,t.isdisabled,t.view,t.reach,t.review,t.created
             from post t
             left join site_user s on s.site_userid=t.userid
             left join album a on a.albumid=t.album
             where t.isdeleted<>'1'
             and t.title like '%$title%'
             and s.user_email like '%$email%'
             and a.caption like '%$album%'
             order by t.created desc limit $offset,$pageSize";


        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return array("total"=>$total,"rows"=>$data);
    }
    public  function saveInfo($id,$userid,$album,$description,$url,$tags,$price,$isdisabled){

        if($id==""){

            return $this->add($userid,$album,$description,$url,$tags,$price,$isdisabled);

        }
        else{

            return $this->alter($id,$userid,$album,$description,$url,$tags,$price,$isdisabled);
        }

    }
    private function add($userid,$album,$description,$url,$tags,$price,$isdisabled){

        try {

            list($insert_id, $total_rows)= DB::insert('post')

            ->columns(array('userid','album','description','url','tags','price','isdisabled','created'))
                
            ->values(array($userid,$album,$description,$url,$tags,$price,$isdisabled,date("Y-m-d h:i:sa")))->execute();

            Database::instance()->commit();

            return json_encode(array("info"=>"success","count"=>$total_rows,"id"=>$insert_id));
        }
        catch (Exception $e) {

            Database::instance()->rollback();

            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
    private function alter($id,$userid,$album,$description,$url,$tags,$price,$isdisabled){

        try {
            $total_rows=DB::update('post')

            ->set(array('userid'=>$userid,'album'=>$album,
                'description'=>$description,'url'=>$url,
                'tags'=>$tags,'price'=>$price,'isdisabled'=>$isdisabled))

            ->where('postid','=',$id)->execute();

            Database::instance()->commit();

            return json_encode(array("info"=>"success","count"=>$total_rows));
        }
        catch (Exception $e) {

            Database::instance()->rollback();

            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
     public function getAlbumInfo($caption,$email,$special,$offset,$pageSize){

        $sql="select count(1)total
             from album t
             left join site_user s on s.site_userid=t.userid
             where t.isdeleted<>'1'
             and t.caption like '%$caption%'
             and s.user_email like '%$email%'
             and t.special='$special'";

        $total=DB::query(Database::SELECT,$sql)->execute()->get("total");

        $sql="select t.albumid id,s.fullname user_name,s.user_email,t.caption,t.image,t.special,t.isdisabled,t.createdon
             from album t
             left join site_user s on s.site_userid=t.userid
             where t.isdeleted<>'1'
             and t.caption like '%$caption%'
             and s.user_email like '%$email%'
             and t.special='$special'
             order by t.createdon desc limit $offset,$pageSize";


        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return array("total"=>$total,"rows"=>$data);
    }
    public function saveAlbumInfo($id,$userid,$caption,$special,$isdisabled){

        if($id==""){

            try {

                list($insert_id, $total_rows)= DB::insert('album')

                ->columns(array('caption','special','isdisabled','userid','createdon'))
                    
                ->values(array($caption,$special,$isdisabled,$userid,date("Y-m-d h:i:sa")))->execute();

                Database::instance()->commit();

                return json_encode(array("info"=>"success","count"=>$total_rows,"id"=>$insert_id));
            }
            catch (Exception $e) {

                Database::instance()->rollback();

                return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
            }

        }
        else{

            try {
                $total_rows=DB::update('album')

                ->set(array('caption'=>$caption,'special'=>$special,'isdisabled'=>$isdisabled,'userid'=>$userid))

                ->where('albumid','=',$id)->execute();

                Database::instance()->commit();

                return json_encode(array("info"=>"success","count"=>$total_rows));
            }
            catch (Exception $e) {

                Database::instance()->rollback();

                return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
            }
        }
    }
    public function getNewInfo(){

        $sql="select t.postid id,s.user_email,a.caption album,t.title,t.description,t.tags,t.images,t.url,t.price,t.isdisabled,t.view,t.reach,t.review,t.created
             from post t
             left join site_user s on s.site_userid=t.userid
             left join album a on a.albumid=t.album
             where t.isdeleted<>'1'
             order by t.created desc limit 0,20";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return $data;
    }
    public function uploadImage($id,$filepath,$filename){
            
            $upFilePath='temp/post/post'.$id."-M-".$filename;

            $model=new Model_Admin_Common();

            $result=$model->upload_file($filepath,$upFilePath,'');

            if($result){

                $img = Image::factory($upFilePath);

                $i ='temp/post/post'.$id."-M500-".$filename;

                $img->resize(500, NULL)->save($i);

                $i ='temp/post/post'.$id."-M260-".$filename;

                $img->resize(260, NULL)->save($i);

                $i ='temp/post/post'.$id."-M100-".$filename;

                $img->resize(100, NULL)->save($i);

                $sql="select images
                    from post t
                    where  t.postid='$id'";

                $images=DB::query(Database::SELECT,$sql)->execute()->get("images");

                if(empty($images)){

                    $images= $filename; 
                }

                else{

                    $images=$images.";".$filename; 

                }

                $total_rows=DB::update('post')->set(array('images'=>$images))

                ->where('postid','=',$id)->execute();

                 Database::instance()->commit();

                return true;
            }
    }

}